package com.bawei.myapp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bawei.myapp.CarActivity;
import com.bawei.myapp.R;
import com.bawei.myapp.entity.CarEntity;
import com.bumptech.glide.Glide;

import java.util.List;

public class CarAdapter extends BaseAdapter {
    private List<CarEntity.DataBean> mDataBeanList;
    private Context mContext;

    public CarAdapter(List<CarEntity.DataBean> dataBeanList, Context context) {
        mDataBeanList = dataBeanList;
        mContext = context;
    }



    @Override
    public int getCount() {
        return mDataBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return mDataBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    //优化
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_car,null);
            viewHolder = new ViewHolder();
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.cb = convertView.findViewById(R.id.cb);
        viewHolder.iv = convertView.findViewById(R.id.iv);
        viewHolder.ivJia = convertView.findViewById(R.id.iv_jia);
        viewHolder.ivJian = convertView.findViewById(R.id.iv_jian);
        viewHolder.ivDelete = convertView.findViewById(R.id.iv_delete);
        viewHolder.tvDesc = convertView.findViewById(R.id.tv_desc);
        viewHolder.tvPrice = convertView.findViewById(R.id.tv_price);
        viewHolder.tvCarNum  = convertView.findViewById(R.id.tv_carNum);


        //展示数据
        viewHolder.cb.setChecked(mDataBeanList.get(position).isSelect());//是否选中
        Glide.with(mContext).load(mDataBeanList.get(position).getGoods_icon())
                .into(viewHolder.iv);//图片
        viewHolder.tvDesc.setText(mDataBeanList.get(position).getGoods_desc()+"");//商品描述
        viewHolder.tvPrice.setText("原价：¥"+mDataBeanList.get(position).getGoods_price()+"");//商品价格
        viewHolder.tvCarNum.setText(mDataBeanList.get(position).getCar_num()+"");//购物车数量


        //事件：选择和取消选择，+，-，删除
        viewHolder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mDataBeanList.get(position).setSelect(isChecked);
                notifyDataSetChanged();
                //总价计算
                CarActivity carActivity = (CarActivity) mContext;//上下文就是activity
                carActivity.total();
            }
        });
        viewHolder.ivJia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num = mDataBeanList.get(position).getCar_num()+1;
                mDataBeanList.get(position).setCar_num(num);
                notifyDataSetChanged();
                //总价计算
                CarActivity carActivity = (CarActivity) mContext;//上下文就是activity
                carActivity.total();
            }
        });
        viewHolder.ivJian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //判断不能减到-1
                int num = mDataBeanList.get(position).getCar_num()-1;
                if(num == -1){
                    Toast.makeText(mContext, "不能再减了", Toast.LENGTH_SHORT).show();
                    return;
                }
                mDataBeanList.get(position).setCar_num(num);
                notifyDataSetChanged();
                //总价计算
                CarActivity carActivity = (CarActivity) mContext;//上下文就是activity
                carActivity.total();
            }
        });
        viewHolder.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDataBeanList.remove(position);
                notifyDataSetChanged();
                //总价计算
                CarActivity carActivity = (CarActivity) mContext;//上下文就是activity
                carActivity.total();

            }
        });


        return convertView;
    }





    class ViewHolder{
        CheckBox cb;
        ImageView iv,ivDelete,ivJia,ivJian;
        TextView tvDesc,tvPrice,tvCarNum;
    }
}
